package com.yhyvr.api.service.store;

import com.github.pagehelper.PageHelper;
import com.google.common.collect.Lists;
import com.lz.mybatis.jdbc.auto.dao.BoGoodsBannerMapper;
import com.lz.mybatis.jdbc.auto.dao.BoGoodsMapper;
import com.lz.mybatis.jdbc.auto.dao.BoTasteGoodsMapper;
import com.lz.mybatis.jdbc.auto.model.*;
import com.lz.tool.ListUtils;
import com.yhyvr.pojo.req.api.ApiReqData;
import com.yhyvr.pojo.req.api.goods.ApiGoodsBannerReq;
import com.yhyvr.pojo.req.api.goods.ApiGoodsDetailReq;
import com.yhyvr.pojo.req.api.goods.ApiGoodsListReq;
import com.yhyvr.pojo.resp.api.ApiRespData;
import com.yhyvr.pojo.resp.api.ApiResultCode;
import com.yhyvr.pojo.resp.api.goods.ApiGoodsDetailResp;
import com.yhyvr.pojo.resp.api.goods.GoodsBannerResp;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * Created by zhangqin on 2017/3/8.
 */

@Service
public class ApiGoodsService {
    @Resource
    private BoGoodsMapper boGoodsMapper;
    @Resource
    private BoTasteGoodsMapper boTasteGoodsMapper;
    public ApiRespData<List<ApiGoodsDetailResp>> goodsList(ApiReqData<ApiGoodsListReq> apiReqBodyApiReqData){
        ApiGoodsListReq apiPagingReq = apiReqBodyApiReqData.getBody();
        PageHelper.startPage(apiPagingReq.getPageNumber(),apiPagingReq.getPageSize());
            BoGoodsExample boGoodsExample = new BoGoodsExample();
        BoGoodsExample.Criteria criteria = boGoodsExample.createCriteria();
            if(StringUtils.isNotBlank(apiPagingReq.getTypeId())){
                criteria.andGoodsTypeIdEqualTo(apiPagingReq.getTypeId());
            }
        criteria.andStateEqualTo(1);
        boGoodsExample.setOrderByClause("sort asc");
        List<BoGoods> list1 = boGoodsMapper.selectByExample(boGoodsExample);
        List<ApiGoodsDetailResp> listResps = Lists.newArrayList();
            for (BoGoods boGoods : list1) {
                listResps.add(getdr(boGoods,apiReqBodyApiReqData.getHead().getClient()));
        }
        return ApiRespData.buildSucc(listResps);
    }


    public ApiRespData<ApiGoodsDetailResp> goodsInfo(ApiReqData<ApiGoodsDetailReq> apiReqBodyApiReqData){
        ApiGoodsDetailReq apiPagingReq = apiReqBodyApiReqData.getBody();
        BoGoods boGoods = boGoodsMapper.selectByPrimaryKey(apiPagingReq.getGoodsId());
        if(boGoods!=null){
            if(apiPagingReq.getGoodsSallType()==5){
                BoTasteGoodsExample boTasteGoodsExample = new BoTasteGoodsExample();
                boTasteGoodsExample.createCriteria().andBoGoodsIdEqualTo(boGoods.getGoodsId()).andBoStatusEqualTo((short) 1).andBoEndDateGreaterThan(new Date());
                BoTasteGoods first = ListUtils.getFirst(boTasteGoodsMapper.selectByExample(boTasteGoodsExample));
                if(first!=null){
                    boGoods.setGoodsPrice(first.getTastePrice());
                    boGoods.setGoodsInventory(first.getBoRemainNum());
                }

            }
            return ApiRespData.buildSucc(getdr(boGoods,apiReqBodyApiReqData.getHead().getClient()));
        }else{
            return ApiRespData.buildFail(ApiResultCode.FAIL,"没有找到该商品信息");
        }
    }

    public ApiRespData<List<ApiGoodsDetailResp>> hotList(ApiReqData<ApiGoodsListReq> apiReqBodyApiReqData){
        ApiGoodsListReq apiPagingReq = apiReqBodyApiReqData.getBody();
        PageHelper.startPage(apiPagingReq.getPageNumber(),apiPagingReq.getPageSize());

        BoTasteGoodsExample boTasteGoodsExample=new BoTasteGoodsExample();
        boTasteGoodsExample.createCriteria().andBoStatusEqualTo((short)1);
        List<BoTasteGoods> tasteGoodsList = boTasteGoodsMapper.selectByExample(boTasteGoodsExample);

        List<ApiGoodsDetailResp> listResps = Lists.newArrayList();
        BoGoodsExample boGoodsExample = new BoGoodsExample();
        BoGoodsExample.Criteria criteria = boGoodsExample.createCriteria();
        for (BoTasteGoods boTasteGoods : tasteGoodsList){
            //criteria.andGoodsIdEqualTo(boTasteGoods.getBoGoodsId());
            BoGoods boGoods=boGoodsMapper.selectByPrimaryKey(boTasteGoods.getBoGoodsId());
            boGoods.setGoodsPrice(boTasteGoods.getTastePrice());
            listResps.add(getdr(boGoods,apiReqBodyApiReqData.getHead().getClient()));

        }
        /***
        BoGoodsExample boGoodsExample = new BoGoodsExample();
        BoGoodsExample.Criteria criteria = boGoodsExample.createCriteria();
        if(StringUtils.isNotBlank(apiPagingReq.getTypeId())){
            criteria.andGoodsTypeIdEqualTo(apiPagingReq.getTypeId());
        }
        criteria.andStateEqualTo(1);
        criteria.andRecommendEqualTo(1);
        boGoodsExample.setOrderByClause("sort asc");
        List<BoGoods> list1 = boGoodsMapper.selectByExample(boGoodsExample);
        List<ApiGoodsDetailResp> listResps = Lists.newArrayList();
        for (BoGoods boGoods : list1) {
            listResps.add(getdr(boGoods,apiReqBodyApiReqData.getHead().getClient()));
        }
         **/
        return ApiRespData.buildSucc(listResps);
    }

    /**
     * 商品封装
     * @param boGoods
     * @param c
     * @return
     */
    public ApiGoodsDetailResp getdr(BoGoods boGoods, String c){
        ApiGoodsDetailResp apiGoodsDetailResp = new ApiGoodsDetailResp();
        apiGoodsDetailResp.setGoodsPrice(boGoods.getGoodsPrice().toString());
        apiGoodsDetailResp.setGoodsOldPrice(boGoods.getGoodsOriginalPrice().toString());
        apiGoodsDetailResp.setGoodsId(boGoods.getGoodsId());
        apiGoodsDetailResp.setGoodsDes(boGoods.getGoodsDescription());
        apiGoodsDetailResp.setGoodsTitle(boGoods.getGoodsTitle());
        apiGoodsDetailResp.setGoodsImage(boGoods.getGoodsImage());
        apiGoodsDetailResp.setRemainNum(boGoods.getGoodsInventory().toString());
        return apiGoodsDetailResp;
    }

    @Resource
    private BoGoodsBannerMapper boGoodsBannerMapper;

    public ApiRespData<List<GoodsBannerResp>> list(ApiReqData<ApiGoodsBannerReq> apiReqData){
        List<GoodsBannerResp> bannerResps = Lists.newArrayList();
        BoGoodsBannerExample commonBannerQuery = new BoGoodsBannerExample();
        ApiGoodsBannerReq bannerReq = apiReqData.getBody();
        commonBannerQuery.createCriteria().andStatusEqualTo(1).andGoodsIdEqualTo(bannerReq.getGoodsId());
        commonBannerQuery.setOrderByClause("sort asc");
        List<BoGoodsBanner> commonBanners = boGoodsBannerMapper.selectByExample(commonBannerQuery);
        if(commonBanners!=null){
            for (BoGoodsBanner commonBanner : commonBanners) {
                GoodsBannerResp bannerResp = new GoodsBannerResp();
                bannerResp.setBannerImage(commonBanner.getBannerImg());
                bannerResps.add(bannerResp);
            }
        }
        return ApiRespData.buildSucc(bannerResps);
    }

}
